package com.czl.lunt.service.impl;

import com.czl.lunt.Exception.ApplicationException;
import com.czl.lunt.common.Result;
import com.czl.lunt.common.ResultCode;
import com.czl.lunt.dao.ArticleMapper;
import com.czl.lunt.model.Article;
import com.czl.lunt.model.Board;
import com.czl.lunt.model.User;
import com.czl.lunt.service.ArticleService;
import com.czl.lunt.service.BoardService;
import com.czl.lunt.service.UserService;
import com.czl.lunt.utils.StringUtils;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    private static final Logger log = LoggerFactory.getLogger(ArticleServiceImpl.class);
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private BoardService boardService;
    @Autowired
    private UserService userService;

    @Override
    public void create(@NonNull Article article) {
        if(article.getUserId() == null
            || StringUtils.isEmpty(article.getTitle())
            || StringUtils.isEmpty(article.getContent())) {
            log.info("发布帖子内容存在问题");
        }
        article.setVisitCount(0); // 访问数
        article.setReplyCount(0); // 回复数
        article.setLikeCount(0);  // 点赞数
        article.setDeleteState((byte) 0);
        article.setState((byte) 0);
        Date date = new Date();
        article.setCreateTime(date);
        article.setUpdateTime(date);
        //更新帖子
        int end = articleMapper.insertSelective(article);
        if(end <= 0){
            log.info("插入失败");
            throw new ApplicationException(Result.failed(ResultCode.FAILED_BOARD_BANNED));
        }
        //更新用户发帖数
        User user = userService.selectById(article.getUserId());
        if(user == null){
            log.info("没有找到指定用户信息");
            throw new ApplicationException("发帖失败");
        }
        userService.addOneArticleById(user.getId());
        //更新板块发帖数
        Board board = boardService.selectById(article.getBoardId());
        if(board == null){
            log.info("没有找到指定板块信息");
            throw new ApplicationException("发帖失败");
        }
        boardService.addOneArticleById(board.getId());
        log.info("发帖成功{} : {}", article.getBoardId(), article.getTitle());
    }

    @Override
    public List<Article> selectAll() {
        return articleMapper.selectAll();
    }

    @Override
    public List<Article> selectAllByBoardId(@NonNull Long boardId) {
        if (boardId < 0){
            log.info("boardId = {}", boardId);
            throw new ApplicationException("帖子id异常");
        }
        Board board = boardService.selectById(boardId);
        if(board == null){
            log.info("访问的板块不存在");
            throw new ApplicationException("板块异常");
        }
        return articleMapper.selectAllByBoardId(boardId);
    }

    @Override
    @Transactional
    public Article selectDetailById(@NonNull Long id) {
        if (id < 0){
            log.info("boardId = {}", id);
            throw new ApplicationException("帖子id异常");
        }
        Article article = articleMapper.selectDetailById(id);
        if(article == null){
            log.info("帖子不存在");
            throw new ApplicationException("帖子不存在");
        }
        Article updateArticle = new Article();
        updateArticle.setId(id);
        updateArticle.setVisitCount(article.getVisitCount() + 1);
        int end = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(end != 1){
            log.info("受影响的行数不等于1");
            throw new ApplicationException("受影响的行数不等于1");
        }
        article.setVisitCount(article.getVisitCount() + 1);
        return article;
    }

    @Override
    public void modify(@NonNull Long id, @NonNull String title, @NonNull String content) {
        if(id < 0 || StringUtils.isEmpty(title) || StringUtils.isEmpty(content)){
            log.info("修改不合法");
            throw new ApplicationException("修改不合法");
        }
        Article updateArticle = new Article();
        updateArticle.setId(id);
        updateArticle.setTitle(title);
        updateArticle.setContent(content);
        updateArticle.setUpdateTime(new Date());
        int end = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(end != 1){
            log.info("影响行数不为1");
            throw new ApplicationException("影响行数异常");
        }
    }

    @Override
    public Article selectById(@NonNull Long id) {
        if (id < 0){
            log.info("boardId = {}", id);
            throw new ApplicationException("帖子id异常");
        }
        return articleMapper.selectDetailById(id);
    }

    @Override
    public void thumbsUpById(@NonNull Long id) {
        if (id < 0){
            log.info("boardId = {}", id);
            throw new ApplicationException("帖子id异常");
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getState() != 0 || article.getDeleteState() != 0){
            throw new ApplicationException("帖子状态异常");
        }
        Article updateArticle = new Article();
        updateArticle.setId(id);
        updateArticle.setLikeCount(article.getLikeCount() + 1);
        updateArticle.setUpdateTime(new Date());
        int end = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(end != 1){
            log.info("影响行数不为1");
            throw new ApplicationException("影响行数异常");
        }


    }

    @Override
    public void addOneReplyById(@NonNull Long id) {
        if (id < 0){
            log.info("boardId = {}", id);
            throw new ApplicationException("帖子id异常");
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getState() != 0 || article.getDeleteState() != 0){
            log.info("article不存在 或 article状态异常");
            throw new ApplicationException("article不存在 或 article状态异常");
        }
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setReplyCount(article.getReplyCount() + 1);
        int end = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(end != 1){
            log.info("回复失败");
            throw new ApplicationException("回复失败");
        }

    }



    @Override
    public void deleteById(@NonNull Long id) {
        if (id < 0){
            log.info("boardId = {}", id);
            throw new ApplicationException("帖子id异常");
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getState() != 0 || article.getDeleteState() != 0){
            log.info("article不存在 或 article状态异常");
            throw new ApplicationException("article不存在 或 article状态异常");
        }
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setDeleteState((byte) 1);

        int end = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(end != 1){
            log.info("删除失败");
            throw new ApplicationException("删除失败");
        }
        boardService.subOneArticleById(article.getBoardId());
        userService.subOneArticleById(article.getUserId());
        log.info("帖子删除成功{}",article.getId());

    }

}